package com.rongshu.view.discovery.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.discovery.adapter.EditorRecommendArticleListAdapter;
import com.rongshu.view.discovery.bean.ArticleListItemBean;
import com.rongshu.view.discovery.bean.ArticleListResponse;
import com.rongshu.view.discovery.bean.SearchArticleResponse;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.discovery.presenter.ArticleListPresenter;
import com.rongshu.view.home.bean.RecommendEditResponse;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2018/11/1.
 * description：编辑推荐的文章列表页面
 */
public class EditorArticleListActivity extends BaseActivity<ArticleListPresenter> implements INetView {


    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.head_right)
    TextView headRight;
    @BindView(R.id.head_content)
    RelativeLayout headContent;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshView;
    @BindView(R.id.no_data_img)
    ImageView noDataImg;
    @BindView(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    private RecommendEditResponse.ResponseBean bean;
    private int pageIndex = 1;
    private int pageSize = 10;
    private List<ArticleListItemBean> articleList;
    private EditorRecommendArticleListAdapter adapter;

    @Override
    protected int initLayout() {
        return R.layout.activity_article_list;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            bean = (RecommendEditResponse.ResponseBean) intent.getSerializableExtra("articleBean");
        }
        if (bean == null) {
            refreshView.setEnableRefresh(true);
            refreshView.setEnableLoadmore(false);
        }
        headMiddle.setText(bean == null ? "文章" : bean.getTitle());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        articleList = new ArrayList<>();
        adapter = new EditorRecommendArticleListAdapter(this, articleList);
        recyclerView.setAdapter(adapter);
        adapter.setAdapterListener(new AdapterListener() {
            @Override
            public void callBack(Object object) {
                ArticleListItemBean response = (ArticleListItemBean) object;
                presenter.toArticleDetailsAct(response);

            }
        });
    }

    @Override
    protected void initData() {
        setPresenter(new ArticleListPresenter(this), this);
        refreshView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageIndex = 1;
                refreshlayout.resetNoMoreData();
                presenter.getData(bean.getId(), pageIndex, pageSize);
            }
        });
        refreshView.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (articleList.size() < pageSize) {
                    refreshlayout.finishLoadmoreWithNoMoreData();
                }
                pageIndex++;
                presenter.getData(bean.getId(), pageIndex, pageSize);
            }
        });
        refreshView.autoRefresh();
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick(R.id.head_left)
    public void onViewClicked() {
        finish();
    }


    @Override
    public void success(Object object, int tag) {
        ArticleListResponse response = (ArticleListResponse) object;
        if (response == null) {
            return;
        }
        if (pageIndex == 1) {
            articleList.clear();
        }
        if (refreshView.isLoading()) {
            refreshView.finishLoadmore();
        }
        if (refreshView.isRefreshing()) {
            refreshView.finishRefresh();
        }
        List<ArticleListItemBean> list = response.getResponse();
        articleList.addAll(list);
        adapter.notifyDataSetChanged();
        noDataLayout.setVisibility(articleList.size() == 0 ? View.VISIBLE : View.GONE);
        if (list.size() == 0) {
            refreshView.finishLoadmoreWithNoMoreData();
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }
}
